package com.hooya.fa.eu.biz.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.hooya.fa.eu.biz.dao.entity.eu.his.TempXsjl;
import com.hooya.fa.eu.biz.dao.mapper.eu.his.HisTempXsjlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/3/19
 * @Version 1.0.0
 */
@Slf4j
public class XsHisListener implements ReadListener<TempXsjl> {

    private HisTempXsjlMapper hisTempXsjlMapper;

    private static final int BATCH_COUNT = 100;

    private List<TempXsjl> cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private Date createTime;

    public XsHisListener(HisTempXsjlMapper hisTempXsjlMapper) {
        this.hisTempXsjlMapper = hisTempXsjlMapper;
        this.createTime = new Date();
    }

    @Override
    public void invoke(TempXsjl data, AnalysisContext context) {
        data.setCreateTime(createTime);
        cacheData.add(data);
        if (cacheData.size() >= BATCH_COUNT) {
            addTempXsjlList();
            cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        addTempXsjlList();
    }

    public void addTempXsjlList() {
        if (!CollectionUtils.isEmpty(cacheData)) {
            hisTempXsjlMapper.insertBatchSomeColumn(cacheData);
        }
    }
}
